package me.wangqu.weiyouqu.web.rest;

import com.codahale.metrics.annotation.Timed;
import me.wangqu.weiyouqu.domain.CaptchaImage;
import me.wangqu.weiyouqu.repository.CaptchaImageRepository;
import me.wangqu.weiyouqu.security.AuthoritiesConstants;
import me.wangqu.weiyouqu.web.rest.util.HeaderUtil;
import me.wangqu.weiyouqu.web.rest.util.PaginationUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing CaptchaImage.
 */
@RestController
@RequestMapping("/api")
public class CaptchaImageResource {

    private final Logger log = LoggerFactory.getLogger(CaptchaImageResource.class);

    @Inject
    private CaptchaImageRepository captchaImageRepository;

    /**
     * POST  /captcha-images : Create a new captchaImage.
     *
     * @param captchaImage the captchaImage to create
     * @return the ResponseEntity with status 201 (Created) and with body the new captchaImage, or with status 400 (Bad Request) if the captchaImage has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @RequestMapping(value = "/captcha-images",
        method = RequestMethod.POST,
        produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    @Secured(AuthoritiesConstants.ADMIN)
    public ResponseEntity<CaptchaImage> createCaptchaImage(@Valid @RequestBody CaptchaImage captchaImage) throws URISyntaxException {
        log.debug("REST request to save CaptchaImage : {}", captchaImage);
        if (captchaImage.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("captchaImage", "idexists", "A new captchaImage cannot already have an ID")).body(null);
        }
        CaptchaImage result = captchaImageRepository.save(captchaImage);
        return ResponseEntity.created(new URI("/api/captcha-images/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert("captchaImage", result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /captcha-images : Updates an existing captchaImage.
     *
     * @param captchaImage the captchaImage to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated captchaImage,
     * or with status 400 (Bad Request) if the captchaImage is not valid,
     * or with status 500 (Internal Server Error) if the captchaImage couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @RequestMapping(value = "/captcha-images",
        method = RequestMethod.PUT,
        produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    @Secured(AuthoritiesConstants.ADMIN)
    public ResponseEntity<CaptchaImage> updateCaptchaImage(@Valid @RequestBody CaptchaImage captchaImage) throws URISyntaxException {
        log.debug("REST request to update CaptchaImage : {}", captchaImage);
        if (captchaImage.getId() == null) {
            return createCaptchaImage(captchaImage);
        }
        CaptchaImage result = captchaImageRepository.save(captchaImage);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert("captchaImage", captchaImage.getId().toString()))
            .body(result);
    }

    /**
     * GET  /captcha-images : get all the captchaImages.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of captchaImages in body
     * @throws URISyntaxException if there is an error to generate the pagination HTTP headers
     */
    @RequestMapping(value = "/captcha-images",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    @Secured(AuthoritiesConstants.ADMIN)
    public ResponseEntity<List<CaptchaImage>> getAllCaptchaImages(Pageable pageable)
        throws URISyntaxException {
        log.debug("REST request to get a page of CaptchaImages");
        Page<CaptchaImage> page = captchaImageRepository.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/captcha-images");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /captcha-images/:id : get the "id" captchaImage.
     *
     * @param id the id of the captchaImage to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the captchaImage, or with status 404 (Not Found)
     */
    @RequestMapping(value = "/captcha-images/{id}",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    @Secured(AuthoritiesConstants.ADMIN)
    public ResponseEntity<CaptchaImage> getCaptchaImage(@PathVariable Long id) {
        log.debug("REST request to get CaptchaImage : {}", id);
        CaptchaImage captchaImage = captchaImageRepository.findOne(id);
        return Optional.ofNullable(captchaImage)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /captcha-images/:id : delete the "id" captchaImage.
     *
     * @param id the id of the captchaImage to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @RequestMapping(value = "/captcha-images/{id}",
        method = RequestMethod.DELETE,
        produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    @Secured(AuthoritiesConstants.ADMIN)
    public ResponseEntity<Void> deleteCaptchaImage(@PathVariable Long id) {
        log.debug("REST request to delete CaptchaImage : {}", id);
        captchaImageRepository.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("captchaImage", id.toString())).build();
    }

}
